﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataTreeLib;
using LiveSequence.Common.Domain;
using LiveSequence.Common.Tree;
using LiveSequence.Interface;
using LiveSequence.Suite.Commands;
using LiveSequence.Suite.Documents;
using LiveSequence.Suite.Domain;
using LiveSequence.Suite.Interfaces;
using LiveSequence.Suite.Controls;
using System.Drawing;

namespace LiveSequence.Suite.Views
{
  public class MethodCallTreeNodeMapper : ITreeNodeMapper
  {
    private MethodCallTreeView _view;

    public MethodCallTreeNodeMapper(MethodCallTreeView view)
    {
      _view = view;
    }

    #region ITreeNodeMapper Members

    public void UpdateNode(object dataNode, TreeNode treeNode)
    {
      DTreeNode<object> dn = (DTreeNode<object>)dataNode;
      treeNode.Tag = dn;

      MethodCallInfo methodCallInfo = dn.Value as MethodCallInfo;
      treeNode.Checked = methodCallInfo.Visible;
      string strText = string.Format("{0}::{1}", methodCallInfo.MethodCallType, methodCallInfo.MethodCallName);
      treeNode.Text = strText;
      Color bkColor = Color.Black;
      if(!methodCallInfo.Visible)
      {
        bkColor = Color.Gray;
      }

      treeNode.ForeColor = bkColor;
      //treeNode.ImageIndex = Convert.ToInt32(dn.Value.SelectionType); //treeNode.Level;
      //treeNode.SelectedImageIndex = Convert.ToInt32(dn.Value.SelectionType);
    }

    public object GetNodeInfo(TreeNode treeNode)
    {
      return treeNode.Tag;
    }
    #endregion

    #region ITreeNodeMapper Members


    public ContextMenuStrip GetContextMenuStrip(TreeNode treeNode)
    {
      ContextMenuStrip result = new ContextMenuStrip();
      if (_view.SelectedDocument != null)
      {
        SeqDiagramDoc document = _view.SelectedDocument;
        ICommand[] commands = new ICommand[]
                                {
                                  new CreateDiagramCommand(document),
                                  new ExtendDiagramCommand(document),
                                  new SplitDiagramCommand(document)
                                };
        foreach (ICommand command in commands)
        {
          DTreeNode<object> dTreeNode = GetNodeInfo(treeNode) as DTreeNode<object>;
          MethodCallInfo methodCallInfo = dTreeNode.Value as MethodCallInfo;
          ToolStripMenuItem mnuItem = new ToolStripCommandItem(command, methodCallInfo);
          result.Items.Add(mnuItem);
        }
      }
      return result;
    }

    #endregion
  }
}
